package com.lsh.airticket.controller.admin;

import com.lsh.airticket.dto.ResponseTicket;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Ticket;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/19
 **/
@RestController
@RequestMapping("/flights")
public class AdminTicketController {


    @Autowired
    TicketService ticketService;

    /**
     * 通过 flightId 查找所有的 机票
     *
     * @param flightId
     * @return
     */
    @GetMapping("/admin/ticket/findTicketByFlightId")
    public Result findTicketByFlightId(String flightId) {
        List<ResponseTicket> responseTicket = ticketService.findResponseTicket(flightId);
        return Result.result().addData("responseTicket", responseTicket);
    }

    /**
     * 插入一条数据
     * @param ticket
     * @return
     */
    @PostMapping("/admin/ticket/insertTicket")
    public Result insertTicket(@RequestBody Ticket ticket) {

        int i = ticketService.insertTicket(ticket);
        if (i != 1) {
            throw new UserException("添加失败");
        }

        return Result.result().setMsg("添加成功");
    }

    /**
     * 根据主键删除一条数据
     */
    @GetMapping("/admin/ticket/deleteTicket")
    public Result deleteTicket(String ticketId) {
        int delete = ticketService.deleteTicketById(ticketId);
        if (delete != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }

    /**
     * 根据主键查询一条数据
     */
    @GetMapping("/admin/ticket/findTicketByTicketId")
    public Result findTicketByTicketId(Integer ticketId) {
        Ticket ticket = ticketService.findTicketById(ticketId);

        return Result.result().addData("ticket", ticket);
    }

    /**
     * 修改一条数据
     */
    @PostMapping("/admin/ticket/updateTicket")
    public Result updateTicket(@RequestBody Ticket ticket) {
        System.out.println(ticket);

        int i = ticketService.updateTicket(ticket);
        if (i != 1) {
            throw new UserException("修改失败");
        }

        return Result.result().setMsg("修改成功");
    }
}
